package cn.zmall.manager.controller;

import cn.zmall.common.pojo.Result;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.manager.dto.OrderInfo;
import cn.zmall.manager.pojo.TbOrder;
import cn.zmall.manager.service.OrderManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author cxy
 * @Date 2020/12/2 10:27
 * @Version 1.0
 */
@RestController
@Api(value = "订单管理")
public class OrderManagerController {

    @Autowired
    private OrderManagerService orderManagerService;

    @RequestMapping(value = "/admin/delorder", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除订单")
    public Result<Object> delOrder(String orderId) {
        int result = orderManagerService.delOrder(orderId);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "admin/cancelorder", method = RequestMethod.POST)
    @ApiOperation(value = "取消订单")
    public Result<Object> cancelOrder(String orderId) {
        int result = orderManagerService.cancelOrder(orderId);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/admin/deliverorder", method = RequestMethod.POST)
    @ApiOperation(value = "订单发货")
    public Result<Object> deliverOrder(String orderId, String shippingName, String shippingCode, BigDecimal postFee) {
        int result = orderManagerService.deliverOrder(orderId, shippingName, shippingCode, postFee);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/admin/salesreturn", method = RequestMethod.POST)
    @ApiOperation(value = "退货")
    public Result<Object> salesReturn(String orderId) {
        int result = orderManagerService.salesReturn(orderId);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/admin/remarkorder", method = RequestMethod.POST)
    @ApiOperation(value = "订单备注")
    public Result<Object> remarkOrder(String orderId, String message) {
        int result = orderManagerService.remarkOrder(orderId, message);
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/admin/getorderdetail", method = RequestMethod.GET)
    @ApiOperation(value = "获取订单详情")
    public Result<OrderInfo> getOrderDetail(String orderId) {
        OrderInfo result = orderManagerService.getOrderDetail(orderId);
        if (result == null) {
            return new ResultUtil<OrderInfo>().setData(null);
        }
        return new ResultUtil<OrderInfo>().setData(result);
    }

    @RequestMapping(value = "/admin/countorder", method = RequestMethod.GET)
    @ApiOperation(value = "获取订单数目")
    public Result<Object> countOrder() {
        Long result = orderManagerService.countOrder();
        if (result == null) {
            return new ResultUtil<Object>().setData(Long.valueOf(-1));
        }
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/admin/getorderlist", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部订单")
    public Result<List<TbOrder>> getOrderList() {
        List<TbOrder> result = orderManagerService.getOrderList();
        return new ResultUtil<List<TbOrder>>().setData(result);
    }
}
